#!/usr/bin env python
# -*- coding: utf-8 -*-
###########################################################################
#   Copyright (C) 2008 by Serj Poltavsky                                  #
#   serge.uliss at gmail.com                                              #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 3 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program. If not, see <http://www.gnu.org/licenses/>   #
###########################################################################

import os.path
from dvdobject import DvdObject
from PIL import Image, ImageDraw, ImageFont

class Css(object):
    pass

class DvdMenuButton(DvdObject):
    _count = 0
    def __init__(self, title, pos, size = (180, 120)):
        DvdObject.__init__(self, 'pal')
        DvdMenuButton._count += 1
        self.count = DvdMenuButton._count
        self.title = title
        self.size = size
        self.pos = pos
        self.thumb = None

        self.border_width = 2
        self.color = '#FFAA00'
        self.font_size = 30
        self.font_family = r"/usr/share/fonts/corefonts/verdana.ttf"

        self.select = Css()
        self.select.color = '#FF0000'
        self.highlight = Css()
        self.highlight.color = '#00FF00'
        #self.highlight.background_color = '#AAAAAA'

    def draw_border(self, draw, style):
        width = self.get_attr(style, 'border_width')
        if not width:
            return
        color = self.get_attr(style, 'border_color')
        if color is None:
            color = self.get_attr(style, 'color')
        background_color = self.get_attr(style, 'background_color')
        print color, width

        self.draw_rect(draw, 0, 0, self.size[0], self.size[1], color, width)

    def draw_rect(self, draw, x0, y0, x1, y1, color, width):
        if width == 1:
            x1 -= 1
            y1 -= 1
        draw.line([x0, y0, x1, y0], width=width, fill=color)
        draw.line([x1, y0, x1, y1], width=width, fill=color)
        draw.line([x1, y1, x0, y1], width=width, fill=color)
        draw.line([x0, y1, x0, y0], width=width, fill=color)

    def draw_text(self, draw, style):
        font_family = self.get_attr(style, 'font_family')
        font_size = self.get_attr(style, 'font_size')
        color = self.get_attr(style, 'color')
        pos = [0, 0]
        font = ImageFont.truetype(font_family, font_size)
        align = self.get_attr(style, 'text_align', 'center')
        if align == 'center':
            bbox = draw.textsize(self.title, font=font)
            pos[0] = (self.size[0] - bbox[0])/2
        elif align == 'right':
            bbox = draw.textsize(self.title, font=font)
            pos[0] = self.size[0] - bbox[0]
        # undocumented hack to disable font antialias
        if style != 'normal':
            draw.fontmode = "1" 
        draw.text(pos, self.title, font=font, fill=color)

    def draw_thumb(self, im):
        if self.thumb is None:
            return

        thumb = Image.open(self.thumb)
        self.im.paste()

    def export(self):
        self.export_normal()
        self.export_highlight()
        self.export_select()

    def export_highlight(self):
        img = Image.new("P", self.size)
        draw = ImageDraw.ImageDraw(img)
        draw.rectangle([0, 0, self.size[0], self.size[1]], fill="#000")
        self.draw_border(draw, 'highlight')
        self.draw_text(draw, 'highlight')
        img.save(self.tmp_highlight(), "PNG", transparency=0)

    def export_normal(self):
        img = Image.new("RGBA", self.size)
        draw = ImageDraw.ImageDraw(img)
        self.draw_border(draw, 'normal')
        self.draw_thumb(draw)
        self.draw_text(draw, 'normal')
        img.save(self.tmp_normal(), "PNG")

    def export_select(self):
        img = Image.new("P", self.size)
        draw = ImageDraw.ImageDraw(img)
        draw.rectangle([0, 0, self.size[0], self.size[1]], fill="#000")
        self.draw_border(draw, 'select')
        self.draw_text(draw, 'select')
        img.save(self.tmp_select(), "PNG", transparency=0)

    def get_attr(self, css, name, default=None):
        attr = default
        if hasattr(self, name):
            attr = getattr(self, name)
        if not hasattr(self, css):
            return attr
        css = getattr(self, css)
        if hasattr(css, name):
            attr = getattr(css, name)
        return attr

    def get_border_width(self):
        return self.__border_width

    def get_pos(self):
        return self.__pos

    def get_size(self):
        return self.__size

    def get_title(self):
        return self.__title

    def set_border_width(self, width):
        self.__border_width = int(width)

    def set_size(self, size):
        self.__size = size

    def set_pos(self, pos):
        self.__pos = pos

    def set_title(self, title):
        self.__title = title

    def tmp_normal(self):
        return os.path.join(self.tmp_dir(), "button_%02i_highlight.png" % self.count)

    def tmp_highlight(self):
        return os.path.join(self.tmp_dir(), "button_%02i_highlight.png" % self.count)

    def tmp_select(self):
        return os.path.join(self.tmp_dir(), "button_%02i_select.png" % self.count)

    border_width = property(get_border_width, set_border_width)
    title = property(get_title, set_title)
    size = property(get_size, set_size)
    pos = property(get_pos, set_pos)